package com.xxxx.server.controller;

import com.xxxx.server.pojo.Admin;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.service.IAdminService;
import com.xxxx.server.service.IOssService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminInfoController {
    /*
    更新用户信息
     */
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IOssService ossService;
    @ApiOperation("更新用户信息")
    @PutMapping("/info")
    public RespBean updateAdminInfo(@RequestBody Admin admin, Authentication authentication)
    {
        if(adminService.updateById(admin))
        {
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,
                    null, authentication.getAuthorities()));
            return RespBean.success("更新成功!");
        }
     return RespBean.error("更新失败");
    }
    @ApiOperation(value = "更新用户密码")
    @PutMapping("/admin/pass")
    public RespBean updateAdminPassword(@RequestBody Map<String,Object> info){
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer adminId = (Integer) info.get("adminId");
        return adminService.updatePassword(oldPass,pass,adminId);
    }
    @ApiOperation(value = "更新用户头像")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "头像", dataType = "MultipartFile")})
    @PostMapping("/admin/userface")
    public RespBean updateHrUserFace(MultipartFile file, Integer id, Authentication authentication) {
        //获取上传文件地址
        String url = ossService.uploadfile(file);
        return adminService.updateAdminUserFace(url, id, authentication);
    }
}
